<?php

namespace Api\Cola;

use Api\Middleware\ColaAuthMiddleware;
use Api\Request\Cola\ColaLedgerRequest;
use App\Cola\Service\ColaLedgerService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Mockery\Exception;

#[Controller]
#[Middleware(ColaAuthMiddleware::class)]
class LedgerController extends ColaBaseController
{

    /**
     * @var ColaLedgerService
     */
    #[Inject]
    protected ColaLedgerService $service;


    /**
     * 账单列表
     * */
    #[GetMapping('list')]
    public function list()
    {
        try {
            $params = [
                'user_id' => colaUser()->getId(),
            ];

            [$page, $size] = $this->getPageSize();

            $field = ['*'];
            $result = $this->service->getApiList($params, $field, $page, $size);

            return $this->response->success('success', $result);

        } catch (Exception $exception) {
            return $this->response->error($exception->getMessage());
        }
    }

    /**
     * 账单列表
     * */
    #[GetMapping('dataList')]
    public function dataList(): ?\Psr\Http\Message\ResponseInterface
    {
        try {
            $params = [
                'user_id' => colaUser()->getId(),
            ];

            [$page, $size] = $this->getPageSize();

            $field = ['id','title'];
            $result = $this->service->getApiList($params, $field, $page, $size);

            return $this->response->success('success', $result);

        } catch (Exception $exception) {
            return $this->response->error($exception->getMessage());
        }
    }


    /**
     * 账单列表
     * */
    #[GetMapping('detail')]
    public function detail(ColaLedgerRequest $request)
    {
        try {
            $params = [
                'user_id' => colaUser()->getId(),
                'id' => $request->input('id'),
                'is_default' => $request->input('is_default'),
            ];

            $field = ['*'];
            $result = $this->service->getApiDetail($params, $field);

            return $this->response->success('success', $result);

        } catch (Exception $exception) {
            return $this->response->error($exception->getMessage());
        }
    }

    /**
     * 添加/修改账单
     * */
    #[PostMapping('save')]
    public function save(ColaLedgerRequest $request)
    {
        try {
            $params = [
                'user_id' => colaUser()->getId(),
                'id' => $request->input('id', 0),
                'title' => $request->input('title'),
                'img' => $request->input('img'),
                'is_default' => $request->input('is_default'),
            ];

            $this->service->apiSave($params);

            return $this->response->success('success');

        } catch (Exception $exception) {
            return $this->response->error($exception->getMessage());
        }
    }

    /**
     * 删除
     * */
    #[PostMapping('del')]
    public function del(ColaLedgerRequest $request)
    {
        try {
            $params = [
                'user_id' => colaUser()->getId(),
                'id' => $request->input('id', 0),
            ];

            $this->service->apiDel($params);
            return $this->response->success('success');

        } catch (Exception $exception) {
            return $this->response->error($exception->getMessage());
        }
    }

    /**
     * 账单明细加入账单
     * */
    #[PostMapping('joinBill')]
    public function joinBill(ColaLedgerRequest $request)
    {
        try {
            $user_id = colaUser()->getId();
            $ledger_id = $request->input('id', 0);
            $bill_id = $request->input('bill_id', 0);

            $this->service->JoinBillService($user_id, $ledger_id, $bill_id);
            return $this->response->success('success');

        } catch (Exception $exception) {
            return $this->response->error($exception->getMessage());
        }
    }

}